<?php

namespace bc\controller;

use yii\web\Session;
use backend\service\system\MenuService;
use bc\wx\OAuth;
use bc\util\Util;
use bc\model\shop\ShopUserModel;

class BackendController extends BaseController
{
	const  PAGE_SIZE = 15;
	
	public $enableCsrfValidation = false;
	
	public $layout = 'default';
	
	public $left_nav;
	
	public $defaultNameSpace = 'backend\\controllers\\';
	
	
	protected $actionName;
	
	protected $controllerName;
	
	public function nolayout() {
		$this->layout = false;
	}
	
	
	public function init()
	{
		parent::init();
		$session = new Session();
		if($session->get('admin') == null)
		{
			$params['msg'] = '您还未登录，请先登录!';
			$params['url'] = '/index/login';
			$this->forward($params);
		}
	}
	

	public function beforeAction($action)
	{
		
		//菜单设置
		$session = new Session();
		if(!$session->get('nav'))
		{
			$service = new MenuService();
			$list = $service->getMenuList();
			$session->set('nav', $list);
		}
		$nav = $session->get('nav');
		$this->left_nav = $nav;
		
		//权限判断
		$this->authCheck($action);
		
		return parent::beforeAction($action);
		
	}
	
	
	/**
	 * 微信授权代码
	 * @param $scope OAuth::SNSAPI_BASE | OAuth::SNSAPI_USERINFO
	 */
	protected function wechatAuth($scope)
	{
		
		$session = new Session();
		$openid = $session->get('openid');
		$code = $this->get('code', '');
		
		if(!$openid)
		{
			if($code)
			{
				//获取用户状态
				$appid = $this->wechat()->appid;
				$secret = $this->wechat()->secret;
				$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
				$rs = Util::httpGet($url);
				$arr = json_decode($rs, true);
					
				$shopModel = new ShopUserModel();
				$user = $shopModel->getRow(['wxid'=>$arr['openid']]);
				if($user)
				{
					$session->set('wxuser', $user);
					$session->set('openid', $arr['openid']);
				}
			}
			else
			{
				//跳转授权拿用户信息
				$auth = new OAuth();
				$redirectUri = $this->getFullUrl();
				$auth->authorize($redirectUri, OAuth::SNSAPI_BASE);
			}
		}
		else
		{
			$oauth = new OAuth();
			$redirectUri = $this->getFullUrl();
			$oauth->authorize($redirectUri, OAuth::SNSAPI_BASE);
		}
		
	}
	
	protected function recursionNav($data, $element = '')
	{
		$nav = '';
		$li = '';
		foreach ($data as $item)
		{
			$li .= '<li><a href="'. $item['url'] .'" menuid="'. $item['id'] .'">';
			$li .= '<i class="fa fa-navicon">&nbsp;&nbsp;</i>';
			$li .= $item['name'];
			if(!empty($item['child']))
			{
				$li .='<span class="fa arrow"></span></a>';
				$li .= '<ul class="nav nav-second-level collapse" aria-expanded="false" style="height: 0px;">';
				$li .= $this->recursionNav($item['child'], $li);
				$li .= '</ul>';
			}
			$li .= '</a>';
			$li .= '</li>';
		}
		$nav .= $li;
		return $nav;
	}
	
	/**
	 * 设置页面锚点
	 * @param array $breadcrumbs
	 * 
	 * array(
	 *			array('url'=>'#', 'name'=>'用户列表'),
	 *			array('url'=>'#', 'name'=>'用户添加'),
	 *	);
	 */
	protected function setBreadcrumbs($breadcrumbs)
	{
		$this->getView()->params['breadcrumbs'] = $breadcrumbs;
	}
	
	
	/**
	 * 判断用户是否有权限操作
	 * @param Action $action the action to be executed.
	 * @return boolean
	 */
	protected function authCheck($action)
	{
		$controller = 	$this->defaultNameSpace . ucfirst($action->controller->id) . 'Controller';//controller name
		$actionName = $action->id;//action name
		
		$action = 'action';
		$arrAction = explode('-', $actionName);
		foreach ($arrAction as $name)
		{
			$action .= ucfirst($name);
		}
		
// 		echo $controller . '\\' . $action;
		
	}

	/**
	 * Created by Mr.hu.
	 * @param $msg
	 * Description:ajax 请求成功
	 */
	final protected function success($msg,$url='')
	{
		exit(json_encode(array('status'=>1,'msg'=>$msg,'url'=>$url),0));
	}

	/**
	 * Created by Mr.hu.
	 * @param $msg
	 * Description:ajax 请求失败
	 */
	final protected function error($msg,$url='')
	{
		exit(json_encode(array('status'=>0,'msg'=>$msg,'url'=>$url),0));
	}
	
	
}